//
//  MasonryTestingController.m
//  LearnObjectiveC
//
//  Created by 吴可高 on 16/6/14.
//  Copyright © 2016年 吴可高. All rights reserved.
//

#import "MasonryTestingController.h"
#import "UtilityTool.h"
@interface TestView : UIView
{
    UIImageView * _bannerImgView;
    UILabel * _productNameLabel;
    UILabel *_describeProductLabel;
}

@end

@implementation TestView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setUp];
    }
    return self;
}
-(void)setUp
{
    _bannerImgView    = [[UIImageView alloc]init];
    _bannerImgView.backgroundColor = [UIColor whiteColor];
    _bannerImgView.layer.masksToBounds = YES;
    _bannerImgView.layer.cornerRadius = 4.f;
    [self addSubview:_bannerImgView];
    [_bannerImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).offset(10);
        make.left.mas_equalTo(self.mas_left).offset(10);
        make.right.mas_equalTo(self.mas_right).offset(-10);
        make.bottom.mas_equalTo(self.mas_bottom).offset(- kScreen_Width);
    }];
    //
    UIView * bannerContentView = [UIView new];
    bannerContentView.backgroundColor = [UIColor redColor];
    bannerContentView.layer.cornerRadius =10.f;
    bannerContentView.layer.masksToBounds = YES;
    [_bannerImgView addSubview:bannerContentView];
    
    [bannerContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_bannerImgView.mas_top).offset(10);
        make.left.mas_equalTo(_bannerImgView.mas_left).offset(10);
        make.right.mas_equalTo(_bannerImgView.mas_right).offset(-10);
        make.bottom.mas_equalTo(_bannerImgView.mas_bottom).offset(-10);
        
    }];
    NSArray * items = @[@"item0",@"item1",@"item2"];
    NSMutableArray * labels = [NSMutableArray array];
    for (int idx = 0;  idx < items.count; idx ++) {
        UILabel * label = [UILabel new];
        label.textColor = [UIColor blueColor];
        label.backgroundColor = [UIColor whiteColor];
        label.text = items[idx];
        label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label];
        [labels addObject:label];
    }
    __block  CGFloat kLabelPadding = 8.f;
    [labels enumerateObjectsUsingBlock:^(UILabel * label, NSUInteger idx, BOOL * stop) {
           [label mas_makeConstraints:^(MASConstraintMaker *make)
             {
                NSInteger i = idx == labels.count-1 ? idx +1 : idx;
                make.left.mas_equalTo(self.mas_left).offset(10 + 50*i + kLabelPadding * idx);
                make.top.mas_equalTo(_bannerImgView.mas_bottom).offset(2*kLabelPadding);
                make.size.mas_equalTo(CGSizeMake(50 *(idx+1), 18));
            }];
    }];
    //
    UILabel * themeLabel = [UILabel new];
    themeLabel.textAlignment = NSTextAlignmentLeft;
    themeLabel.textColor = [UIColor blueColor];
    themeLabel.text = @"《边城》是沈从文的代表作，入选20世纪中文小说100强，排名第二位，仅次于鲁迅的《呐喊》。它以20世纪30年代川湘交界的边城小镇茶峒为背景，以兼具抒情诗和小品文的优美笔触，描绘了湘西地区特有的风土人情；";
    themeLabel.numberOfLines = 0.f;
    themeLabel.font = [UIFont systemFontOfSize:13];
    [themeLabel sizeToFit];
    [self addSubview:themeLabel];
    
    __block UILabel * label = nil;
    label = labels[0];
    [themeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(label.mas_leading).offset(5.f);
        make.top.mas_equalTo(label.mas_bottom).offset(15.f);
        make.right.mas_lessThanOrEqualTo(self.mas_right).offset(-20);
        make.right.mas_greaterThanOrEqualTo(self.mas_right).priority(MASLayoutPriorityDefaultHigh);
    }];
    //
    UILabel * descLabel = [UILabel new];
    descLabel.textColor = [UIColor yellowColor];
    descLabel.textAlignment = NSTextAlignmentLeft;
    descLabel.numberOfLines = 0.f;
    [descLabel sizeToFit];
    descLabel.text = @"借船家少女翠翠的纯爱故事，展现出了人性的善良美好。由于《边城》的美学艺术，《边城》这部小说在中国近代文学史上具有独特的地位。《边城》是沈从文的代表作，入选20世纪中文小说100强，排名第二位，仅次于鲁迅的《呐喊》。它以20世纪30年代川湘交界的边城小镇茶峒为背景，以兼具抒情诗和小品文的优美笔触，描绘了湘西地区特有的风土人情";
    descLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:descLabel];

    [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(themeLabel.mas_left).offset(2.f);
        make.top.mas_equalTo(themeLabel.mas_bottom).offset(10);
        make.right.mas_lessThanOrEqualTo(self.mas_right).offset(-20);
        make.right.mas_greaterThanOrEqualTo(self.mas_right).priority(MASLayoutPriorityDefaultHigh);
    }];
    // 测试 lessThan  greaterThan 她们的长度关系是系数k ＝ 1 2 3f;
    NSMutableArray * views = [NSMutableArray array];
    
    for (int i = 0 ; i < 3 ; i++) {
        UIView * view = [[UIView alloc]init];
        view.backgroundColor = [UIColor whiteColor];
        view.layer.masksToBounds = YES;
        view.layer.cornerRadius = 3.f;
        [self addSubview:view];
        [views addObject:view];
    }
    CGFloat kViewPadding = 8.f;
    CGFloat kViewWidth = ((kScreen_Width - 40)- 4 * kViewPadding)/6;
    [views enumerateObjectsUsingBlock:^(UIView * view, NSUInteger idx, BOOL * stop) {
        NSInteger i = idx == views.count-1 ? idx +1 : idx;
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(descLabel.mas_left).offset(2.f + (kViewWidth* i) + kViewPadding * idx);
            make.top.mas_equalTo(descLabel.mas_bottom).offset(15);
            make.height.mas_equalTo(50.f);
            make.width.mas_equalTo(kViewWidth * (idx+1));
        }];
    }];
}

@end

@interface MasonryTestingController ()
{
    TestView * _testView;
}

@end
@implementation MasonryTestingController

#pragma mark ViewController LifeCycle
- (void)viewDidLoad
{
    [super viewDidLoad];
     self.view.backgroundColor  = [UIColor whiteColor];
    [self  setViewControllerNavigationBarStyle];
    [self initSubObjectView];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.tabBarController.tabBar setHidden:NO];
}


#pragma mark ViewController AsynRequestData

#pragma mark ViewController Algorithm Processor

#pragma mark ViewController Initialize SubObjectView

-(void)initSubObjectView
{
     TestView * testView = [[TestView alloc]init];
       testView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:testView];
     _testView = testView;
    [_testView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view.mas_left).offset(10);
        make.top.mas_equalTo(self.view.mas_top).offset(kNavigationBarHeight+10);
        make.right.mas_equalTo(self.view.mas_right).offset(-10);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-10);
    }];
}

-(void)setViewControllerNavigationBarStyle
{
    [self customNavigationBarStyleWithleftBarItems:@[@"back",@""]
                               navigationItemTitle:@"masonryViewController"
                            andRightBarButtonItems:@[@"share",@""]
                         leftBarItemHandleComplete:^(NSInteger idx, UIButton *parameter) {
                         }
                        rightBarItemHanbleComplete:^(NSInteger idx, UIButton *parameter) {
                            
                        }];
}
#pragma mark --viewController getter methods


#pragma mark Possess  Delegate ViewController（委托代理者）
#pragma mark IBOutlet Action ViewController

#pragma mark Other Method
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
}


@end
